package controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.EventDAO;
import model.MessageDAO;
import model.UserDAO;
import beans.CommentBean;
import beans.EventBean;
import beans.UserBean;

/**
 * Servlet implementation class AttendEventServlet
 */
@WebServlet("/EventActionServlet")
public class EventActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EventActionServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{	    
			HttpSession session = request.getSession();
			UserBean user = (UserBean)session.getAttribute("currentSessionUser");
			EventBean event = (EventBean)session.getAttribute("currentEvent");
			if (request.getParameter("action").equals("attend"))
				EventDAO.attendEvent(user, event);
			else if (request.getParameter("action").equals("remove"))
				EventDAO.leaveEvent(user, event);
			else if (request.getParameter("action").equals("flyer"))
				EventDAO.updateFlyerURL(event, request.getParameter("newURL"));
			else if (request.getParameter("action").equals("rate")) {
				if (!user.getEmail().equals(event.getHost())) {
					int rate = Integer.valueOf(request.getParameter("ratehost"));
					user.setGuestToHostRating(rate);
					EventDAO.rate(user, event);
					System.out.println("Rating the host: a nice " + rate);
				} else {
					int[] rates = new int[event.getGuestList().size()];
					for (int i = 0; i < event.getGuestList().size(); i++) {
						rates[i] = Integer.valueOf(request.getParameter("rate" + i));
						event.getGuestList().get(i).setHostToGuestRating(rates[i]);
						System.out.println("Rating the guest #" + i + " : a nice " + rates[i]);
					}
					EventDAO.rate(user, event);	
				}
			}
			else if (request.getParameter("action").equals("edit")) {
				if (user.getEmail().equals(event.getHost()) || user.getAdmin())
					session.setAttribute("editing", true);
					response.sendRedirect("eventcreation.jsp");
					return;
			}
			else if (request.getParameter("action").equals("delete")) {
				if (user.getAdmin())
					EventDAO.delete(event);
					response.sendRedirect("LoginServlet");
					return;
			}
			String start = event.getStartTimeStamp().toString();
			start = start.substring(0, 19);
			start = start.replace("-", "");
			start = start.replace(":", "");
			start = start.replace(" ", "");
			System.out.println(start);
			response.sendRedirect("EventServlet?host=" + event.getHost() + "&start=" + start);
		} 


		catch (Throwable theException) 	    
		{
			System.out.println(theException); 
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
